using Microsoft.VisualStudio.Package;
using Microsoft.WowAddonStudio.Lua.LanguageService.Model;
using Microsoft.WowAddonStudio.Lua.Parser;

namespace Microsoft.WowAddonStudio.Lua.LanguageService.Support
{
    /// <summary>
    /// LineScanner wraps the GPLEX scanner to provide the IScanner interface
    /// required by the Managed Package Framework. This includes mapping tokens
    /// to color definitions.
    /// </summary>
    public class LineScanner : IScanner
    {
        private readonly IColorScan lexer = new Scanner();

        /// <summary>
        /// Scans the token and provide info about it.
        /// </summary>
        /// <param name="tokenInfo">The token info.</param>
        /// <param name="state">The state.</param>
        /// <returns></returns>
        public bool ScanTokenAndProvideInfoAboutIt(TokenInfo tokenInfo, ref int state)
        {
            int start, end;
            int token = lexer.GetNext(ref state, out start, out end);

            // !EOL and !EOF
            if (token != (int)Tokens.EOF)
            {
                TokenDefinition definition = Configuration.GetTokenDefinition(token);

                tokenInfo.Token = token;
                tokenInfo.StartIndex = start;
                tokenInfo.EndIndex = end;

                tokenInfo.Color = definition.Color;
                tokenInfo.Type = definition.Type;
                tokenInfo.Trigger = definition.Triggers;

                return true;
            }

            return false;
        }

        /// <summary>
        /// Sets the source.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="offset">The offset.</param>
        public void SetSource(string source, int offset)
        {
            lexer.SetSource(source, offset);
        }
    }
}